//
//  IssueViewController.m
//  CareWithFriends
//
//  Created by Versatile Systems, Inc on 11/6/13.
//  Copyright (c) 2013 Muludiang. All rights reserved.
//

#import "IssueViewController.h"
#import "CWFData.h"
#import "CommentsViewController.h"

@interface IssueViewController ()

@end

@implementation IssueViewController

@synthesize comments, addCommentsBtn, sendToMIRBtn, forwardBTN;
@synthesize nameField, accountTN ,contactField, tagsField, descriptionView;
@synthesize nameFieldtext, accountTNtext,contactFieldtext, tagsFieldtext, descriptionViewtext;
@synthesize commentsTable, tempArray, rowID;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // return number of rows
    //NSLog(@"Rows: %d | rowID: %d",[[[tempArray objectAtIndex:rowID] objectForKey:@"posts"] count] , rowID);
    return 1;  //[[[tempArray objectAtIndex:rowID] valueForKey:@"posts"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PostsCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    [cell.textLabel setText:[NSString stringWithFormat:@"Total posts (%d)",[[[tempArray objectAtIndex:rowID] valueForKey:@"posts"] count]] ];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[[tempArray objectAtIndex:rowID] valueForKey:@"posts"] count] > 0){
        CommentsViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"comments_view"];
        dvc.issueID = [[[tempArray objectAtIndex:rowID] objectForKey:@"id"] integerValue];
        dvc.tempArray = [tempArray objectAtIndex:rowID];
        [self.navigationController pushViewController:dvc animated:YES];
        NSLog(@"Issue ID: %d", [[[tempArray objectAtIndex:rowID] objectForKey:@"id"] integerValue]);
    }
}

-(IBAction)addaPost:(id)sender{
    UIAlertView *postAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Add a post" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit post", nil ];
    [postAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
    postAlert.tag = 1;
    [postAlert show];

}

-(IBAction)sendToMIR:(id)sender{
    UIAlertView *sendToMIRAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Sent to Make it Right Team!" delegate:self cancelButtonTitle:@"Home" otherButtonTitles: nil];
    sendToMIRAlert.tag = 2;
    [sendToMIRAlert show];
}

-(IBAction)forwardIssue:(id)sender{
    if ([MFMailComposeViewController canSendMail])
    {// Email Subject
        NSString *emailTitle = @"An Issue from Care With Friends";
        // Email Content
        NSString *messageBody = @"Care With Friends Issue details...";
        // To address
        NSArray *toRecipents = [NSArray arrayWithObject:@""];
        
        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
        mc.mailComposeDelegate = self;
        [mc setSubject:emailTitle];
        [mc setMessageBody:messageBody isHTML:NO];
        [mc setToRecipients:toRecipents];
        
        // Present mail view controller on screen
        [self presentViewController:mc animated:YES completion:NULL];
        
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
        switch (result)
        {
            case MFMailComposeResultCancelled:
                NSLog(@"Mail cancelled");
                break;
            case MFMailComposeResultSaved:
                NSLog(@"Mail saved");
                break;
            case MFMailComposeResultSent:
                NSLog(@"Mail sent");
                break;
            case MFMailComposeResultFailed:
                NSLog(@"Mail sent failure: %@", [error localizedDescription]);
                break;
            default:
                break;
        }
        
        // Close the Mail Interface
        [self dismissViewControllerAnimated:YES completion:NULL];
    
    if (result == MFMailComposeResultSent){
        UIAlertView *forwardAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Email Sent!" delegate:self cancelButtonTitle:@"Home" otherButtonTitles: nil];
        forwardAlert.tag = 3;
        [forwardAlert show];
        
    }
    
}
    
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 1){
        CWFData *dataObj = [CWFData sharedInstance];
        [dataObj createPost:[alertView textFieldAtIndex:0].text postIssueNum:[[tempArray objectAtIndex:rowID] valueForKey:@"id"] postType:@"Problem"];
        //[dataObj createComment:[alertView textFieldAtIndex:0].text postIssueNum:[[tempArray objectAtIndex:rowID] valueForKey:@"id"]];
    }
    
    
    if ((alertView.tag == 2) || (alertView.tag == 3)){
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    nameField.text = nameFieldtext;
    accountTN.text = accountTNtext;
    contactField.text = contactFieldtext;
    tagsField.text = tagsFieldtext;
    descriptionView.text = descriptionViewtext;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
